package org.example.spring03tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Component
public class AccountDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    /*按照username扣减账号余额
    * username:用户名
    * delete:扣减的金额*/
    @Transactional(propagation = Propagation.REQUIRED)
    public void reduceBalance(String username, BigDecimal delete) {
        String sql = "update account set balance = balance - ? where username = ?";
        // 执行SQL
        jdbcTemplate.update(sql,delete,username);
    }
}
